﻿
Imports System.Reflection
Imports std
Imports centrosphere.job.storage

'TODO: use autoJob
Partial Public Class storageJob
    Inherits basicJob(Of storageJob, String)

    Private Shared ReadOnly field_types() As Type = Nothing

    Shared Sub New()
        Dim fields() As FieldInfo = Nothing
        fields = GetType(storageJob).GetFields(BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.GetField)
        field_types = New Type() {GetType(bytesByte),
                                  GetType(bytesBoolean),
                                  GetType(bytesString),
                                  GetType(bytesbytes),
                                  GetType(bytesVector(Of String, bytesString)),
                                  GetType(bytesInt64),
                                  GetType(bytesInt64)}
        assert(field_types.Length() = fields.Length())
        ReDim command_required(count - 1)
        initial_command_required()
        ReDim command_init(count - 1)
        initial_command_init()
    End Sub

    Public command As Byte = job.command.invalid
    Public result As Boolean = False
    Public key As String = Nothing
    Public buff As bytes = Nothing
    Public vs As bytesVector(Of String, bytesString) = Nothing
    Public i64 As Int64 = 0
    Public i64_2 As Int64 = 0

    Protected Overrides Function job_valid() As Boolean
        Return MyBase.job_valid() AndAlso job.storage.valid(command) AndAlso required(Me)
    End Function

    Protected Overrides Sub receive_lop()
        MyBase.receive_lop()
        If Not keyrequired(command) Then
            key = Nothing
        End If
        If Not buffrequired(command) Then
            buff = Nothing
        End If
        vs = Nothing
    End Sub

    'cannot respond_lop, since the storageJobRedundanceDistributor requires the input data
    'Protected Overrides Sub respond_lop()
    '    MyBase.respond_lop()
    '    If Not keyrespond(command) Then
    '        key = Nothing
    '    End If
    '    If Not buffrespond(command) Then
    '        buff = Nothing
    '    End If
    '    If Not vsrespond(command) Then
    '        vs = Nothing
    '    End If
    'End Sub

    Protected Overrides Function hashcode_key() As String
        Return key
    End Function

    Protected Overrides Sub copy(ByVal i As storageJob)
        assert(Not i Is Nothing)
        copy(command, i.command)
        copy(result, i.result)
        copy(key, i.key)
        copy(buff, i.buff)
        copy(vs, i.vs)
        copy(i64, i.i64)
        copy(i64_2, i.i64_2)
    End Sub

    Protected Overrides Sub frombytes(ByVal paras() As fromBytes)
        Dim index As Int64 = 0
        Dim nextPara As _do(Of fromBytes) =
            Function() As fromBytes
                Return paras(inc_(index))
            End Function
        assert(Not paras Is Nothing AndAlso paras.Length() = field_types.Length())
        command = nextPara().as(Of Byte)()
        result = nextPara().as(Of Boolean)()
        key = nextPara().as(Of String)()
        buff = nextPara().as(Of bytes)()
        vs = nextPara().as(Of bytesVector(Of String, bytesString))()
        i64 = nextPara().as(Of Int64)()
        i64_2 = nextPara().as(Of Int64)()
        assert(index = field_types.Length())
    End Sub

    Protected Overrides Function paras() As toBytes()
        Return New toBytes() {New bytesByte(command),
                              New bytesBoolean(result),
                              New bytesString(key),
                              New bytesbytes(buff),
                              vs,
                              New bytesInt64(i64),
                              New bytesInt64(i64_2)}
    End Function

    Protected Overrides Sub auto_fill()
        MyBase.auto_fill()
        assert(command <> job.command.invalid)
        If job.storage.i64_timestamp(command) Then
            i64 = now_t.global.lowres_ticks()
        End If
    End Sub

    Protected Overrides Function alloc() As storageJob
        Return New storageJob()
    End Function

    Protected Overrides Function types() As Type()
        Return field_types
    End Function
End Class
